var app = getApp();
var setting = app.globalData.setting;
var request = app.request;
var common = require('../../../utils/common.js');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		url: setting.url,
		resourceUrl: setting.resourceUrl,
		codetxt: '获取验证码',
		phone: '',
		code: '',
		address: '',
		remark: '',
		openReasonModal: false,
		index: 0,
		message: null,
		reasonSelect: 0, //选择供货类别
		reasonList: [] //供货类别列表
	},
	goto(e) {
		wx.navigateTo({
			url: e.currentTarget.dataset.url
		});
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.getcategory();
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow(options) {
		if (this.data.message) {
      // console.log(this.data.message, 98)
      // console.log(this.data.message.title, 77)
      this.setData({address:this.data.message.title})
		}
	},
	//获取验证码
	getCode: function() {
		var that = this;
		if (this.data.codetxt == '获取验证码' || this.data.codetxt == '重新发送') {
			let phone = this.data.phone;
			if (phone == '') {
				wx.showModal({
					title: '提示',
					content: '请输入手机号',
					showCancel: false
				})
				return
			} else if (phone.length != 11) {
				wx.showModal({
					title: '提示',
					content: '请输入正确的手机号码',
					showCancel: false
				})
			} else {
				// var total_micro_second = 60;
				// 验证码倒计时
				// that.count_down(total_micro_second);
				// return;
				//检查格式
				common.sendSmsCode(that.data.phone, null, function(res) {
					if (res.data.status == 1) {
						var total_micro_second = 60;
						// 验证码倒计时
						that.count_down(total_micro_second);
					}
				});

				// request.post('/api/User/apply_leader', {
				//   data: {
				//     phone: that.data.phone
				//   },
				//   failRollback: true,
				//   successReload: true,
				//   success: function (res) {


				//   }
				// });       
			}
		} else {
			wx.showModal({
				title: '提示',
				content: '您点的太快了',
				showCancel: false
			})
		}
	},
	// 倒计时 验证码
	count_down: function(total_micro_second) { //发送验证码按钮
		var that = this;
		if (total_micro_second <= 0) {
			that.setData({
				codetxt: "重新发送"
			});
			// timeout则跳出递归
			return false;
		} else {
			// 渲染倒计时时钟
			that.setData({
				codetxt: total_micro_second + "s"
			});
			total_micro_second--;
			setTimeout(function() {
				that.count_down(total_micro_second);
			}, 1000)
		}
	},
	phone(e) {
		this.setData({
			phone: e.detail.value
		})
	},
	code(e) {
		//console.log(e, 22)
		this.setData({
			code: e.detail.value
		})
	},
	address(e) {
    //console.log(e.detail.value,235)
		this.setData({
			address: e.detail.value
		})
	},
	remark(e) {
		this.setData({
			remark: e.detail.value
		})
	},
	//提交
	submit() {
		var that = this;

		request.post('/api/User/apply_shop', {
			data: {
				phone: that.data.phone,
				code: that.data.code,
				address: that.data.address,
				remark: that.data.remark
			},
			failRollback: true,
			successReload: true,
			success: function(res) {
				if (res.data.status == -1) {
					console.log(res);
					app.showTextWarining(res.data.msg);
				} else {
					app.showSuccess('申请成功！');
				}

			}
		});
	},
	getcategory() {
		var that = this;
		request.get('/api/goods/goodsCategoryList', {
			isShowLoading: false,
			data: {
				new_ad: 1
			},
			success: function(res) {
				var categories = res.data.result.category;
				if (categories.length == 0) {
					return;
				}
				that.setData({
					reasonList: categories
				});
			//	console.log(that.data.reasonList, 456)
			}
		});

	},
	openReasonModal: function() {
		this.setData({
			openReasonModal: true
		});
	},

	closeReasonModal: function() {
		this.setData({
			openReasonModal: false
		});
	},

	/** 改变供货类别理由 */
	reasonChange: function(e) {
		this.data.index = e.detail.value;
	},

	confirm: function() {
		this.setData({
			reasonSelect: this.data.index
		});
		this.closeReasonModal();
	},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})